package org.zeroes.Components;

import android.util.Log;
import org.zeroes.Events.*;

import com.zeroes.cons.ZSGameManager;
import com.zeroes.cons.ZSLayer;

public class ZSBLayerScaleTo extends ZSLayerBehavior{

	private float destX;
	private float destY;
	private float diffX;
	private float diffY;
	private float startX;
	private float startY;
	private boolean endedx=false;
	private boolean endedy=false;
	private float  prcntg=0.0f;
	private int dur;
	private int repeats;
	public int ease;
	private int delay;

	private int inc=1;
	private ZSLayer _subj;
	
	public ZSBLayerScaleTo(String name,ZSLayer subject, int delay, int repeats, int _ease){
		super(name,subject);

		this.AddIntegerProperty("*.property.delay", delay);
		this.AddIntegerProperty("*.property.repeats", repeats);
		this.AddIntegerProperty("*.property.ease", ease);
		this.repeats=repeats;
		this.delay=delay;
		this.ease=_ease;
		this._subj=subject;
		this.on(subject.title+".ScaleTo.run", new Callback(){
			
			public void run(String p, ZSEvent e){
				if(e.loopindex>=0)
				{
					Log.v("gtrans"," > "+e.loopindex/dur);
					float stepx=0;
					float stepy=0;
					

					 int stepLength = 17;
					
					
					 float neededframes=(dur/stepLength);
					 if(FrameRateDependant){
						 	stepx=diffX;
							stepy=diffY;
							inc=1;
							prcntg=(float)e.loopindex/dur;
							
					 }
					 else
					 {
					 	stepx=diffX/neededframes;
						stepy=diffY/neededframes;
						prcntg=inc/neededframes;
						endedx= (destX>startX&&(startX+inc*stepx)>destX)||(destX<startX&&(startX+inc*stepx)<destX) ? true : false;
						endedy= (destY>startY&&(startY+inc*stepy)>destY)||(destY<startY&&(startY+inc*stepy)<destY) ? true : false;			
					 }
							
					
					if(!endedx&&!endedy){
						//super.onUpdate(stepLength);
						switch(ease){
						case ZSBehavior.LINEAR :
							_subj.setScale(startX+prcntg*inc*stepx, startY+prcntg*inc*stepy);
						break;
						case ZSBehavior.EASE_IN :
							_subj.setScale(startX+ZSBehavior.cubicin(prcntg)*diffX, startY+ZSBehavior.cubicin(prcntg)*diffY);
						break;	
						case ZSBehavior.EASE_OUT :
							_subj.setScale(startX+ZSBehavior.cubicout(prcntg)*diffX, startY+ZSBehavior.cubicout(prcntg)*diffY);
						break;	
						case ZSBehavior.EASE_IN_OUT :
							_subj.setScale(startX+ZSBehavior.cubicinout(prcntg,dur)*diffX, startY+ZSBehavior.cubicinout(prcntg,dur)*diffY);
						break;	
						case ZSBehavior.BACK_IN :
							_subj.setScale(startX+ZSBehavior.backin(prcntg)*diffX, startY+ZSBehavior.backin(prcntg)*diffY);
						break;
						case ZSBehavior.BACK_OUT :
							_subj.setScale(startX+ZSBehavior.backout(prcntg)*diffX, startY+ZSBehavior.backout(prcntg)*diffY);
						break;
						case ZSBehavior.ELASTIC_IN :
							_subj.setScale(startX+ZSBehavior.elasticin(prcntg,dur/1000)*diffX, startY+ZSBehavior.elasticin(prcntg,dur/1000)*diffY);
						break;
						case ZSBehavior.ELASTIC_OUT :
							_subj.setScale(startX+ZSBehavior.elasticout(prcntg,neededframes)*diffX, startY+ZSBehavior.elasticout(prcntg,neededframes)*diffY);
							//Log.v("gtrans"," > "+(startY+ZSBehavior.elasticout(100*e.loopindex/dur,dur)*diffY));
							break;
						case ZSBehavior.BOUNCE :
							_subj.setScale(startX+ZSBehavior.bounce(prcntg)*diffX, startY+ZSBehavior.bounce(prcntg)*diffY);
						break;
						}
						
						
					}
						
					inc++;
					
				

				
				//_subj.setPosition((startX+(float)e.loopindex/dur*diffX), (startY+(float)e.loopindex/dur*diffY));
				}
					else
				{
						inc =1;
						endedx=false;
						endedy=false;

						
						_subj.setScale(destX, destY);
					
					String[] items = e.name.split("\\.");
					ZSGameManager.AskMe().events._(items[0]+"."+items[1]+".finished", e);
					
				}
			}
		});
		
		
		this.AddAction("*.action.run", new ZSLogicUnit(){
			
			public void is(ZSComponent c, ZSLUParameters p){
				ZSBLayerScaleTo b =(ZSBLayerScaleTo)c;
				
				ZSLayer target = b.getLayerProperty("Subject");
				ZSEventSheet E = ZSGameManager.AskMe().events;
				E._(target.title+".ScaleTo.start", null);
				int delay= b.getIntegerProperty("delay");
				int rep= b.getIntegerProperty("repeats");
				ZSSingle<Integer> sd=(ZSSingle<Integer>)p.getParamAtIndex(0);
				ZSSingle<Float> _x=(ZSSingle<Float>)p.getParamAtIndex(1);
				ZSSingle<Float> _y=(ZSSingle<Float>)p.getParamAtIndex(2);
				String dir=">";
				if(p.getParamsCount()==3)
					dir=">";
				else
				{
					ZSSingle<String> _dir=(ZSSingle<String>)p.getParamAtIndex(3);
					dir=_dir.getFirst();
				}
				if(dir.equals(">")){
				destX=_x.getFirst();
				destY=_y.getFirst();

				startX=target.lsx;
				startY=target.lsy;
				}
				else
				{
					startX=_x.getFirst();
					startY=_y.getFirst();

					destX=target.lsx;
					destY=target.lsy;
					
				}
				diffX=destX-startX;
				diffY=destY-startY;
				dur=sd.getFirst();
				mainCondition=E.makeCustomDuration(sd.getFirst(),delay,rep,target.title+".ScaleTo.run",new Condback(){
					
					public void is(String n, int idx){
						ZSEvent ev = new ZSEvent(n,null,idx);	
						
						ZSGameManager.AskMe().events._(n, ev);
						
					}
				});
				

				
			}
		});
		
	}
}
